﻿using System.Net.Mail;
using System.Text;
using System.Text.RegularExpressions;

namespace Application.Utils
{
    /*如果是25端口：【部署云服务器要解封25端口：腾讯云:头像 > 安全管控 > 25端口解封】
     */
    public class SysEmail
    {
        //private readonly string _server;
        //private readonly string _port;
        //private readonly string _address;
        //private readonly string _authcode;

        private readonly EmailSetting _setting;

        public SysEmail(string server, int port, string address, string authcode)
        {
            if (!server.Has() || port == 0 || !address.Has() || !authcode.Has())
            {
                throw new ArgumentException("邮箱服务配置有误");
            }
            _setting = new EmailSetting
            {
                Host = server,
                Port = port,
                FromMailAddress = address,
                AuthCode = authcode
            };
        }

        //public SysEmail(EmailSetting setting)
        //{
        //    _setting = setting;
        //}
        /// <summary>
        /// 邮件的发送
        /// </summary>
        /// /// <param name="subject">title</param>
        /// <param name="bodyInfo">content</param>
        /// <param name="toEmails">多个用分号隔开;</param>
        /// <returns></returns>
        public async Task<bool> Send(string subject, string bodyInfo, string toEmails, bool IsBodyHtml = false)
        {
            SmtpClient _smtpClient = new SmtpClient();
            _smtpClient.EnableSsl = true;
            _smtpClient.UseDefaultCredentials = false;
            _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            _smtpClient.Host = _setting.Host;
            _smtpClient.Port = _setting.Port;
            _smtpClient.Credentials = new System.Net.NetworkCredential(_setting.FromMailAddress, _setting.AuthCode);

            MailMessage _mailMessage = new MailMessage(_setting.FromMailAddress, toEmails);
            _mailMessage.Subject = subject;//主题  
            _mailMessage.Body = bodyInfo;//内容
            _mailMessage.BodyEncoding = Encoding.Default;//正文编码  
            _mailMessage.IsBodyHtml = IsBodyHtml;//设置为HTML格式
            _mailMessage.Priority = MailPriority.Normal;//优先级  

            await _smtpClient.SendMailAsync(_mailMessage);
            return true;
        }

        public static EmailSetting GetEmailSetting(string emailSetting)
        {
            emailSetting = emailSetting.LastOrDefault() == ';' ? emailSetting : emailSetting + ";";
            string senderServer = new Regex(@"(?<=server=).+?(?=;)").Match(emailSetting).ToString();
            string fromMailAddress = new Regex(@"(?<=address=).+?(?=;)").Match(emailSetting).ToString();
            string Port = new Regex(@"(?<=port=).+?(?=;)").Match(emailSetting).ToString();
            string mailAuthCode = new Regex(@"(?<=authcode=).+?(?=;)").Match(emailSetting).ToString();
            return new EmailSetting
            {
                AuthCode = mailAuthCode,
                FromMailAddress = fromMailAddress,
                Host = senderServer,
                Port = Convert.ToInt32(Port)
            };
        }
        public class EmailSetting
        {
            public string Host { get; set; }
            public int Port { get; set; }
            public string FromMailAddress { get; set; }
            public string AuthCode { get; set; }
        }
    }
}
